﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.intellitack.layar.library
{
    /// <summary>
    /// 
    /// Note About Naming Conventions:
    /// 
    /// The .NET Framework 3.5 includes a JSON Serialization / Deserialization class in System.Web.Script.Serialization
    /// unfortunately the library does not contain a 'rename' attribute.  Since, I want to get going on this
    /// all public members will be lower case, according to the Layars spec.  Once I write a custom Serializer I can 
    /// remedy this situation.
    /// 
    /// Notes About Strings
    /// 
    /// A few notes regarding formatting of the strings to be displayed in the BIW: title, line2, line3, line4 and attributions:
    ///  •Except for the 'title' string, which can wrap over 2 lines, all other strings will be clipped to one line in the client.
    ///  •If there is a chance that your return strings may be too long, please make sure you repeat the information contained in 
    ///   these strings in a detail page that the user can select when choosing one of the actions specified. This way any clipped 
    ///   text can still be read by the user.
    ///   •The special character sequence '%distance%' can be used anywhere in the string and will tell the client to replace it with 
    ///   the actual distance including the unit to the POI. E.g. '%distance% from here' will be displayed '684 meters from here' on the
    ///   client if the actual distance between user and POI is 684 meters and the user has chosen to use the metric system.
    ///   It will be displayed '.42 mi from here' if the user has chosen the statute miles system.
    ///   •Layar is developed to work on many different clients, therefore it is not possible to guarantee string lengths that will
    ///   work on all clients. All clients have a screen width of 320 pixels, and a text box width in the BIW of 180 pixels. 
    ///   Recommended maximum string lengths (including spaces and special characters) that will work on most clients are: 
    ///         ◦title: 60 characters (will be wrapped on two lines)
    ///         ◦line2 - line 4: 35 characters (no wrapping)
    ///         ◦attribution: 45 characters (no wrapping)
    ///   •UTF-8 is fully supported by Layar. Your JSON response can contain UTF-8 encoded strings. Make sure though that you declare 
    ///   your charset accordingly in the HTTP headers:
    ///         Content-type application/javascript; charset=utf-8
    ///   •HTML formatting is not supported. Use UTF-8 for special characters.
    ///   •Use UTF-8 code for the '<' string (\u003C) to avoid a bug in the current APK which will still try to interpret some HTML.
    /// </summary>
    public class PointOfInterest : AbstractLayarObject
    {
        #region Protected Properties

        protected List<POIAction> _actions;
        protected string _attribution;
        protected decimal? _distance;
        protected string _id;
        protected string _imageURL;
        protected decimal? _lat;
        protected decimal? _lon;
        protected string _line2;
        protected string _line3;
        protected string _line4;
        protected string _title;
        protected int? _type;
        
        #endregion

        #region Constructors
        public PointOfInterest() 
        {
            _actions = new List<POIAction>();
        }
        #endregion

        #region Public Properties

        /// <summary>
        /// A list of URIs that can be invoked when the user clicks on the BIW.  The various actions are displayed as a list of buttons in an overlay sheet.
        /// </summary>
        public List<POIAction> actions
        {
            get
            {
                return _actions;
            }
        }

        /// <summary>
        /// The last line in the BIW, small font using textColor.
        /// </summary>
        public string attribution
        {
            get
            {
                return _attribution;
            }
            set
            {
                _attribution = value;
            }
        }

        /// <summary>
        /// The distance of the POI to the current location
        /// </summary>
        public decimal? distance
        {
            get
            {
                return _distance;
            }
            set
            {
                _distance = value;
            }
        }

        /// <summary>
        /// A unique id for the POI within the layer, this is used to track the POIs when refreshing the list.
        /// </summary>
        public string id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        /// <summary>
        /// The location of the image to be dispalyed inside the BIW.  If the image is too large, it will be resized to fit within a bounding box of 100x70 pixels, preserving the aspect ratio.  Pass null if empty.
        /// </summary>
        public string imageURL
        {
            get
            {
                return _imageURL;
            }
            set
            {
                _imageURL = value;
            }
        }

        /// <summary>
        /// The integer value of the latitute for the POI:  divide by 10^6 to get the decimal GPS corrdinate value.  Can be negative.
        /// </summary>
        public decimal? lat
        {
            get
            {
                return _lat;
            }
            set
            {
                _lat = value;
            }
        }

        /// <summary>
        /// The integer value of the longitude ofr the POI:  devide by 10^6 to get the decimal GPS coordinate value.  Can be negative
        /// </summary>
        public decimal? lon
        {
            get
            {
                return _lon;
            }
            set
            {
                _lon = value;
            }
        }

        /// <summary>
        /// The second line in the BIW, normal font using textColor
        /// </summary>
        public string line2
        {
            get
            {
                return _line2;
            }
            set
            {
                _line2 = value;
            }
        }

        /// <summary>
        /// The third line in the BIW, normal font using textColor.
        /// </summary>
        public string line3
        {
            get
            {
                return _line3;
            }
            set
            {
                _line3 = value;
            }
        }

        /// <summary>
        /// The fourth line in the BIW, normal font using textColor.
        /// </summary>
        public string line4
        {
            get
            {
                return _line4;
            }
            set
            {
                _line4 = value;
            }
        }

        /// <summary>
        /// The first line in the BIW, larger font, underlined, using titleColor.
        /// </summary>
        public string title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
            }
        }

        /// <summary>
        /// type of POI, from which the client determines to use if custom CIWs are defined.  0 = default CIW.  1-3 = use the custom CIW icons specified in the layer definition (custom_ciw)
        /// </summary>
        public int? type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }
        }

        #endregion

        public override string Serialize() 
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append(string.Format("{0},", SerializeEnumeration("actions",actions)));
            sb.Append(string.Format("{0},", SerializeProperty("attribution", attribution)));
            sb.Append(string.Format("{0},", SerializeProperty("distance", distance)));
            sb.Append(string.Format("{0},", SerializeProperty("id", id)));
            sb.Append(string.Format("{0},", SerializeProperty("imageURL", imageURL)));
            //091016.DCP - Need to convert the LAT / LON from dec to int 10^6 according to spec.
            sb.Append(string.Format("{0},", SerializeProperty("lat", System.Convert.ToInt32(lat * 1000000))));
            sb.Append(string.Format("{0},", SerializeProperty("lon", System.Convert.ToInt32(lon * 1000000))));
            
            sb.Append(string.Format("{0},", SerializeProperty("line2", line2)));
            sb.Append(string.Format("{0},", SerializeProperty("line3", line3)));
            sb.Append(string.Format("{0},", SerializeProperty("line4", line4)));
            sb.Append(string.Format("{0},", SerializeProperty("title", title)));
            sb.Append(string.Format("{0}", SerializeProperty("type", type)));

            sb.Append("}");
            return sb.ToString();

        }
    }
}
